import java.nio.FloatBuffer;
import com.sun.opengl.util.*;

class TileSystem {

  final static int maxTiles = 19;
  final static int wireframeTiles = 19;

  Tile[] tiles;
  Tile[] wireframe;

  TileSystem() {
    tiles = new Tile[maxTiles];
    for(int i=0; i<maxTiles; i++) {
      tiles[i] = new Tile();
      tiles[i].init(1,1,(int)random(1,6));
      //tiles[i].move((i+1)*50,(i+1)*50);
    }
    wireframe = new Tile[wireframeTiles];
    for(int i=0; i<wireframeTiles; i++) {
      wireframe[i] = new Tile();
      wireframe[i].init(1,1,7);
      switch(i) {
      case 1: 
        wireframe[i].move(width/2,height-50);
        break;
      case 2: 
        wireframe[i].move(width/2,height-150);
        break;
      case 3: 
        wireframe[i].move(width/2,height-250);
        break;
      case 4: 
        wireframe[i].move(width/2,height-350);
        break;
      case 5: 
        wireframe[i].move(width/2,height-450);
        break;
      case 6:
        wireframe[i].move((width/2)-wireframe[i].radius*1.5,height-wireframe[i].radius*8);
        break;
      case 7:
        wireframe[i].move((width/2)-wireframe[i].radius*1.5,height-wireframe[i].radius*6);
        break;
      case 8:
        wireframe[i].move((width/2)-wireframe[i].radius*1.5,height-wireframe[i].radius*4);
        break;
      case 9:
        wireframe[i].move((width/2)-wireframe[i].radius*1.5,height-wireframe[i].radius*2);
        break;
      case 10:
        wireframe[i].move((width/2)+wireframe[i].radius*1.5,height-wireframe[i].radius*2);
        break;
      case 11:
        wireframe[i].move((width/2)+wireframe[i].radius*1.5,height-wireframe[i].radius*4);
        break;
      case 12:
        wireframe[i].move((width/2)+wireframe[i].radius*1.5,height-wireframe[i].radius*6);
        break;
      case 13:
        wireframe[i].move((width/2)+wireframe[i].radius*1.5,height-wireframe[i].radius*8);
        break;
      case 14:
        wireframe[i].move((width/2)+wireframe[i].radius*3,height-wireframe[i].radius*3);
        break;
      case 15:
        wireframe[i].move((width/2)+wireframe[i].radius*3,height-wireframe[i].radius*5);
        break;
      case 16:
        wireframe[i].move((width/2)+wireframe[i].radius*3,height-wireframe[i].radius*7);
        break;
      case 17:
        wireframe[i].move((width/2)-wireframe[i].radius*3,height-wireframe[i].radius*7);
        break;
      case 18:
        wireframe[i].move((width/2)-wireframe[i].radius*3,height-wireframe[i].radius*5);
        break;
      case 0:
        wireframe[i].move((width/2)-wireframe[i].radius*3,height-wireframe[i].radius*3);
        break;
      default: 
        println("Unimplemented Wireframe Tile Reached: Ignoring ID: "+i);
      }
    }
  }

  void fadeToColor(GL gl, float r, float g, float b, float speed) {
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4f(r, g, b, speed);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0, 0);
    gl.glVertex2f(width, 0);
    gl.glVertex2f(width, height*.95);
    gl.glVertex2f(0, height*.95);
    gl.glEnd();
  }

  void fadeBottom(GL gl) {
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4f(0,0,0,1);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0,height*.95);
    gl.glVertex2f(width, height*.95);
    gl.glVertex2f(width,height);
    gl.glVertex2f(0,height);
    gl.glEnd();
  }

  void updateAndDraw(){
    background(0);
    for(int i=0; i<wireframeTiles; i++) {
      PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
      GL gl = pgl.beginGL();
      gl.glEnable(GL.GL_LINE_SMOOTH);
      gl.glLineWidth(2);
      wireframe[i].drawWireframe(gl);
      gl.glEnd();
      gl.glDisable(GL.GL_BLEND);
      pgl.endGL();
    }
    for(int i=0; i<maxTiles; i++) {
      PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;         // processings opengl graphics object
      GL gl = pgl.beginGL();
      gl.glEnable(GL.GL_BLEND);
      gl.glEnable(GL.GL_LINE_SMOOTH);
      gl.glLineWidth(1);
      tiles[i].tex.bind();
      tiles[i].tex.enable();
      tiles[i].drawOldSchool(gl);
      gl.glEnd();
      gl.glDisable(GL.GL_BLEND);
      pgl.endGL();
    }

  }

  boolean onTile() {
    for(int i=0; i<maxTiles; i++) {
      if((mouseX > tiles[i].xpos-tiles[i].radius) & (mouseX < tiles[i].xpos+tiles[i].radius)) {
        if((mouseY > tiles[i].ypos-tiles[i].radius) & (mouseY < tiles[i].ypos+tiles[i].radius)) {
          return true;
        }
      }
    }
    return false;
  }

  int getTile() {
    for(int i=0; i<maxTiles; i++) {
      if((mouseX > tiles[i].xpos-tiles[i].radius) & (mouseX < tiles[i].xpos+tiles[i].radius)) {
        if((mouseY > tiles[i].ypos-tiles[i].radius) & (mouseY < tiles[i].ypos+tiles[i].radius)) {
          return i;
        }
      }
    }
    return 0;
  }

  boolean inFrame() {
    for(int i=0; i<wireframeTiles; i++) {
      if((mouseX > wireframe[i].xpos-wireframe[i].radius) & (mouseX < wireframe[i].xpos+wireframe[i].radius)) {
        if((mouseY > wireframe[i].ypos-wireframe[i].radius) & (mouseY < wireframe[i].ypos+wireframe[i].radius)) {
          return true;
        }
      }
    }
    return false;
  }

  float[] getFrame() {
    float[] x = new float[2];
    for(int i=0; i<wireframeTiles; i++) {
      if((mouseX > wireframe[i].xpos-wireframe[i].radius) & (mouseX < wireframe[i].xpos+wireframe[i].radius)) {
        if((mouseY > wireframe[i].ypos-wireframe[i].radius) & (mouseY < wireframe[i].ypos+wireframe[i].radius)) {
          x[0] = wireframe[i].xpos;
          x[1] = wireframe[i].ypos;
          return x;
        }
      }
      //x[0] = tiles[i].xpos;
      //x[1] = tiles[i].ypos;
    }
    return x;
  }
}














